package main

import "fmt"

// 直接挪原切片
func Delete1(slice []int, index int) []int {
	if index < 0 || index >= len(slice) {
		fmt.Println("下标不合法")
		return slice
	}

	//取最后一个元素下标
	var end = len(slice) - 1
	for index < end {
		slice[index] = slice[index+1]
		index++
	}

	return slice
}

// 使用子切片来实现
func Delete2(slice []int, index int) []int {
	if index < 0 || index >= len(slice) {
		fmt.Println("下标不合法")
		return slice
	}

	left := slice[0:index]
	right := slice[index+1:]
	ret := make([]int, 0, len(slice)-1)

	for i := 0; i < len(left); i++ {
		ret = append(ret, left[i])
	}
	for i := 0; i < len(right); i++ {
		ret = append(ret, right[i])
	}

	return ret
}

// 改造为泛型
func Delete3[T Number](slice []T, index int) []T {
	if index < 0 || index >= len(slice) {
		fmt.Println("下标不合法")
		return slice
	}

	left := slice[0:index]
	right := slice[index+1:]
	ret := make([]T, 0, len(slice)-1)

	for i := 0; i < len(left); i++ {
		ret = append(ret, left[i])
	}
	for i := 0; i < len(right); i++ {
		ret = append(ret, right[i])
	}

	return ret
}

type Number interface {
	int | int64 | int8 | int32 | float32 | float64
}
